package Example.Components;

import java.util.Date;
import java.util.Vector;

import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.BindingController;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

import Example.Interfaces.BQuery;
import Example.Interfaces.BSave;
import Example.Interfaces.BikeOprs;
import Example.Interfaces.CourseOprs;
import Example.Interfaces.LocOprs;
import Example.Interfaces.UQuery;
import Example.Interfaces.USave;


public class BikeTrip implements BikeOprs, CourseOprs, BindingController{


	public Vector selectBike(String locationStart, String locationEnd) {
		// TODO Auto-generated method stub
		Vector bikes=new Vector();
		double distance=0.0;
		if(LocOprs!=null){
			String station=LocOprs.findLoc(locationStart);
			if(station!=null)
				bikes=BQuery.findBike(station);
			else bikes=BQuery.findBike(locationStart);
			distance=LocOprs.calcDistance(locationStart, locationEnd);
		}
		else
			bikes=BQuery.findBike(locationStart);
		
		bikes.add(Double.toString(distance));
		return bikes;
	}


	public boolean returnBike(int bikeID, String location, double trip) {
		BSave.saveBike(bikeID, location, trip);
		return false;
	}


	public boolean rentBike(int bikeID, int userID) {
		boolean b;
		String c;
		b=BSave.saveBike(bikeID, "", 0);
		c=BSave.saveCourse(userID, bikeID);
		if(b)
			return true;
		return false;
	}


	public String endCourse(int userID, int bikeID) {
		return BSave.saveCourse(userID, bikeID);
	}


	public Object[] startCourse(int userID) {
		// TODO Auto-generated method stub
		return BQuery.findCourse(userID);
	}


	public String[] listFc() {
		 return new String[]{"BQuery","BSave","LocOprs"};
	}


	public Object lookupFc(String cItf)
			throws NoSuchInterfaceException {
		if(cItf.equals("BQuery"))
			return BQuery;
		if(cItf.equals("BSave"))
			return BSave;	
		if(cItf.equals("LocOprs"))
			return LocOprs;
		return null;
	}


	public void unbindFc(String cItf) throws NoSuchInterfaceException,
			IllegalBindingException, IllegalLifeCycleException {
		if(cItf.equals("BQuery"))
			BQuery=null;
		if(cItf.equals("BSave"))
			BSave=null;
		if(cItf.equals("LocOprs"))
			LocOprs=null;
		
	}
	private BQuery BQuery;
	private BSave BSave;
	private LocOprs LocOprs;
	
	public void bindFc(String cItf, Object sItf)
			throws NoSuchInterfaceException, IllegalBindingException,
			IllegalLifeCycleException {
		if(cItf.equals("BQuery"))
			BQuery=(BQuery)sItf;
		if(cItf.equals("BSave"))
			BSave=(BSave)sItf;
		if(cItf.equals("LocOprs"))
			LocOprs=(LocOprs)sItf;
	
		
	}
}
